package org.brain.rolephase;

import javax.faces.application.Application;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.brain.pojos.User;

public class RolePhase implements PhaseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5571619884556786602L;

	public void afterPhase(PhaseEvent arg0) {
		// TODO Auto-generated method stub
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
		String url = request.getRequestURI();
		
		if(url.toLowerCase().indexOf("login.jsf")<0)
		{
			HttpSession session = request.getSession();
			User user =  (User) session.getAttribute("user");
			if(user == null)
			{
				Application application = fc.getApplication();
				NavigationHandler nh = application.getNavigationHandler();
				nh.handleNavigation(fc, "", "error");
			}
		}
		

	}

	public void beforePhase(PhaseEvent event) {
		// TODO Auto-generated method stub
		
	}

	public PhaseId getPhaseId() {
		// TODO Auto-generated method stub
		return PhaseId.RESTORE_VIEW;
	}

}
